const Koa = require('koa');
const next = require('next');

const Router = require('koa-router');
const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();
app.prepare().then(() => {
    const server = new Koa();
    const router = new Router();

    router.get('*', async ctx => {
        // console.log(ctx.req, ctx.res, 'ctx.req.cookies')
        // const token = ctx.cookies.get('token')
        // !token && ctx.response.redirect("/login")
        await handle(ctx.req, ctx.res);
        ctx.respond = false;
    });

    server.use(router.routes());

    // 防止出现控制台报404错误
    server.use(async (ctx, next) => {
        ctx.res.statusCode = 200;
        await next();
    });

    server.listen(3001, () => {
        console.log('server is running at http://localhost:3001');
    });
});